package edu.gatech.cs4400.spring2014.A8.view;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import edu.gatech.cs4400.spring2014.A8.control.WaitedOn;
import edu.gatech.cs4400.spring2014.A8.model.PatientData;

/**
 * View/create/edit patient profile view
 * 
 * @author Stephen
 *
 */
public class PatientProfilePanel extends JPanel implements WaitedOn, ActionListener {
	public static enum Feedback{SAVE, CANCEL};
	private static final String saveString = "Save";
	private static final String cancelString = "Cancel";
	private static final long serialVersionUID = 1L;
	private static String[] fieldNames = {"Name", "Date of Birth",
										  "Address", "Home Phone", "Work Phone",
										  "Contact Name", "Contact Phone", 
										  "Weight", "Height", "Annual Income"};
	
	private boolean finished;
	private Feedback feedback;
	private HashMap<String, JTextField> name2field;
	private JComboBox<String> genderBox;
	private PatientData profileData;
	private AllergiesPanel allergiesPanel;
	
	/**
	 * Set up the panel with the given PatientProfileData
	 * 
	 * @param profileData
	 * @param requireSave
	 */
	public PatientProfilePanel(PatientData profileData, boolean requireSave) {
		reset();
		this.profileData = profileData;
		name2field = new HashMap<String, JTextField>();
		JTextField temp;
		
		JPanel panel1 = new JPanel();
		panel1.setLayout(new GridLayout(fieldNames.length+1,2));
		panel1.add(new JLabel("Gender"));
		genderBox = new JComboBox<String>();
		genderBox.addItem("M");
		genderBox.addItem("F");
		panel1.add(genderBox);
		
		for (String name : fieldNames) {
			temp = new JTextField(15);
			name2field.put(name, temp);
			panel1.add(new JLabel(name));
			panel1.add(temp);
		}
		
		fillFields();
		
		allergiesPanel = new AllergiesPanel();
		JPanel panel2 = allergiesPanel;
		
		JPanel panel3 = new JPanel();
		panel3.setLayout(new GridLayout(1,2));
		JButton saveButton = new JButton(saveString);
		saveButton.addActionListener(this);
		panel3.add(saveButton);
		if (!requireSave) {
			JButton cancelButton = new JButton(cancelString);
			cancelButton.addActionListener(this);
			panel3.add(cancelButton);
		}
		
		this.add(panel1);
		this.add(panel2);
		this.add(panel3);
	}
	
	/**
	 * Parse the profile data into the fields
	 */
	public void fillFields() {
		name2field.get("Name").setText(profileData.getName());
		name2field.get("Home Phone").setText(profileData.getHomePhone());
		name2field.get("Address").setText(profileData.getAddress());
		name2field.get("Work Phone").setText(profileData.getWorkPhone());
		name2field.get("Weight").setText("" + profileData.getWeight());
		name2field.get("Height").setText("" + profileData.getHeight());
		name2field.get("Annual Income").setText("" + profileData.getAnnualIncome());
		name2field.get("Contact Name").setText(profileData.getContactName());
		name2field.get("Contact Phone").setText(profileData.getContactPhone());
		if (profileData.getDateOfBirth() != null) {
			name2field.get("Date of Birth").setText(profileData.getDateOfBirth().toString());
		}
		if (profileData.getGender() == "") {
			genderBox.setSelectedItem("M");
		}
		else {
			genderBox.setSelectedItem(profileData.getGender());
		}

	}
	
	/**
	 * Reset the GUI flags if more user input is desired
	 */
	public void reset() {
		finished = false;
		feedback = null;
	}
	
	/**
	 * Get user control input (enumerated)
	 * @return
	 */
	public Feedback getFeedback() {
		return feedback;
	}
	
	/**
	 * Determine if the menu is done and more control decisions need to be made
	 */
	@Override
	public boolean isDone() {
		return finished;
	}
	
	/**
	 * Handle save/cancel buttons
	 */
	@Override
	public void actionPerformed(ActionEvent arg0) {
		finished = true;
		switch (arg0.getActionCommand()) {
		case saveString:
			feedback = Feedback.SAVE;
			saveData();
			break;
		case cancelString:
			feedback = Feedback.CANCEL;
			break;
		}
		synchronized(this) {
			this.notify();
		}
	}
	
	private void saveData() {
		profileData.setName(name2field.get("Name").getText());
		profileData.setHomePhone(name2field.get("Home Phone").getText());
		profileData.setAddress(name2field.get("Address").getText());
		profileData.setWorkPhone(name2field.get("Work Phone").getText());
		profileData.setWeight(name2field.get("Weight").getText());
		profileData.setHeight(name2field.get("Height").getText());
		profileData.setDateOfBirth(name2field.get("Date of Birth").getText());
		profileData.setAnnualIncome(name2field.get("Annual Income").getText());
		profileData.setGender((String)genderBox.getSelectedItem());
		profileData.setContactName(name2field.get("Contact Name").getText());
		profileData.setContactPhone(name2field.get("Contact Phone").getText());
		allergiesPanel.saveData();		
	}
	
	/**
	 * Sub panel to handle the allergies
	 * 
	 * @author Stephen
	 *
	 */
	private class AllergiesPanel extends JPanel implements ActionListener {
		private static final long serialVersionUID = 1L;
		
		private HashMap<JButton, JPanel> button2panel;
		private ArrayList<JPanel> panels;
		private JPanel center;

		/**
		 * Initialize the panel with the title, "+" button, and allergy entries
		 */
		public AllergiesPanel() {
			this.setLayout(new BorderLayout());
			this.add(new JLabel("Allergies"), BorderLayout.NORTH);
			JButton newAllergy = new JButton("+");
			newAllergy.addActionListener(this);
			this.add(newAllergy, BorderLayout.SOUTH);
			center = new JPanel();
			
			ArrayList<String> allergies = profileData.getAllergies();
			if (allergies.size() == 0) {
				allergies.add("");
			}
			center.setLayout(new GridLayout(Math.min(allergies.size(), 9), 1));
			
			panels = new ArrayList<JPanel>();
			button2panel = new HashMap<JButton, JPanel>();
			int count = 0;
			for (String allergy : allergies) {
				JPanel panel = newPanel(allergy);
				center.add(panel);
				count++;
				if (count == 9) {
					break;
				}
			}

			this.add(center, BorderLayout.CENTER);
		}

		/**
		 * 
		 */
		@Override
		public void actionPerformed(ActionEvent arg0) {
			if (arg0.getActionCommand().equals("X")) {
				panels.remove(button2panel.get(arg0.getSource()));
			}
			else if (arg0.getActionCommand().equals("+")) {
				newPanel("");
			}
			remakeAllergies();
		}
		
		private JPanel newPanel(String text) {
			JTextField field = new JTextField(15);
			field.setText(text);
			JPanel panel = new JPanel();
			panels.add(panel);
			JButton button = new JButton("X");
			button.addActionListener(this);
			button2panel.put(button, panel);
			panel.setLayout(new GridLayout(1,2));
			panel.add(field);
			panel.add(button);
			return panel;
		}
		
		/**
		 * Remake the allergies list after an entry is added/removed
		 */
		private void remakeAllergies() {
			center.removeAll();
			if (panels.size() == 0) {
				newPanel("");
			}
			center.setLayout(new GridLayout(Math.min(panels.size(), 9), 1));
			int count = 0;
			for (JPanel panel : panels) {
				center.add(panel);
				count++;
				if (count == 9) {
					break;
				}
			}
			center.revalidate();
		}
		
		private void saveData() {
			ArrayList<String> allergies = new ArrayList<String>();
			for (JPanel panel : panels) {
				JTextField field = (JTextField)panel.getComponent(0);
				allergies.add(field.getText());
			}
			profileData.setAllergies(allergies);
		}
	}

}
